package cdu.bbs.bbsv2.controller;

import cdu.bbs.bbsv2.interceptor.AdminLoginInterceptor;
import cdu.bbs.bbsv2.interceptor.UserLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@Controller
public class WebConfigurer implements WebMvcConfigurer {

    @RequestMapping({"/","index","login"})
    public String index(){
        return "index";
    }

    @RequestMapping({"/admin/","/admin/loginPage",})
    public String adminIndex(){
        return "/admin/loginPage";
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login.html").setViewName("index");

        registry.addViewController("/admin/loginPage.html").setViewName("admin/loginPage");
        registry.addViewController("/admin/userList.html").setViewName("admin/userList");
        registry.addViewController("/admin/topicList.html").setViewName("admin/topicList");

        registry.addViewController("/register.html").setViewName("register");
        registry.addViewController("/help.html").setViewName("help");
        registry.addViewController("/mine.html").setViewName("mine");
        registry.addViewController("/question.html").setViewName("question");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AdminLoginInterceptor())
                .addPathPatterns("/admin/**")
                .excludePathPatterns(
                        "/admin/login",
                        "/admin/loginPage"
                        );

        registry.addInterceptor(new UserLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/admin/**",
                        "/**/index",
                        "/**/login",
                        "/user/getVerifyCode",
                        "/user/register",
                        "/**/*.html",
                        "/**/*.js",
                        "/**/*.css",
                        "/image/**");

    }
}
